
#include "VCPlugInHeaders.h"

#include "IStringListData.h"
#include "ListTreeViewAdapter.h"
#include "AMPNodeID.h"
#include "AMPID.h"

#include "AMGlobal.h"
#include "AMPLogDataRecord.h"
/**
 *  TreeViewAdapter.
 */
class AMPTVHierarchyAdapter : public ListTreeViewAdapter
{
public:
	AMPTVHierarchyAdapter(IPMUnknown* boss);
	
	virtual ~AMPTVHierarchyAdapter()
	{}
	
	virtual int32	GetNumListItems() const;
	NodeID_rv GetRootNode() const;
	virtual NodeID_rv	GetNthListItem(const int32& nth) const;
	
	//virtual void Reload();
	
};

CREATE_PMINTERFACE(AMPTVHierarchyAdapter, kAMPTVHierarchyAdapterImpl)
/********************************************************************************************************************
 *****Function: AMPTVHierarchyAdapter
 *****Creator:  Toan Nguyen
 *****IN: 
 *****out: 
 *****Objective: 
 ********************************************************************************************************************/
AMPTVHierarchyAdapter::AMPTVHierarchyAdapter(IPMUnknown* boss):ListTreeViewAdapter(boss)
{
	AMPUtils::CreateWorkspaceDir();	
	LoadSettingInfo();
	//cout << "LoadSettingInfo" << endl;
	InitLogHeaderFile();
	//cout << "InitLogHeaderFile" << endl;
	//AMPUtils::DoUseResponder();
	int32 i;
	K2Vector<PMString> lists;
	if (g_IsLoggedIn == FALSE)
	{
		//initialize the list with the default list string name
		for (i = 0; i< 2; i++)
		{
			PMString name(kAMPItemBaseKey);
			name.AppendNumber(i+1);
			name.Translate();
			lists.push_back(name);
		}
	}else
	{
		PMString signOutStr(kAMPItemBaseKey);
		signOutStr.AppendNumber(3); // Sign out
		signOutStr.Translate();
		lists.push_back(signOutStr);
		
		PMString browseIssueNodeName(kAMPItemBaseKey);
		browseIssueNodeName.AppendNumber(2); // Browse Issues
		browseIssueNodeName.Translate();
		lists.push_back(browseIssueNodeName);
	}
	
	InterfacePtr<IStringListData> iListData(this, IID_ISTRINGLISTDATA);
	iListData->SetStringList(lists);
	
}

/********************************************************************************************************************
 *****Function: GetNumListItems
 *****Creator:  Toan Nguyen
 *****IN: 
 *****out: 
 *****Objective: 
 ********************************************************************************************************************/
int32 AMPTVHierarchyAdapter::GetNumListItems()const
{	
	InterfacePtr<IStringListData> iListData(this, IID_ISTRINGLISTDATA);
	//cout << "AMPTVHierarchyAdapter::GetNumListItems: " << iListData->GetStringList().size() << endl;
	int32 listCount = iListData->GetStringList().size();
	
	if (g_SelectedTextFrameUID.length() > 0) {
		string HelloUser = "Sign out (" + g_Username + ")" ;
		
		K2Vector<PMString> lists;
		if (g_IsLoggedIn == TRUE) {
			//PMString signOutStr(kAMPItemBaseKey);
			//signOutStr.AppendNumber(3); // Sign out
			PMString signOutStr(HelloUser.c_str()); 
			signOutStr.Translate();
			lists.push_back(signOutStr);
			
			PMString browseIssueNodeName(kAMPItemBaseKey);
			browseIssueNodeName.AppendNumber(2); // Browse Issues
			browseIssueNodeName.Translate();
			lists.push_back(browseIssueNodeName);
			

			// check in idml file
			if (g_DocName.length() > 0) {
				PMString checkInStr(kAMPItemBaseKey);
				checkInStr.AppendNumber(4); // check in
				checkInStr.Translate();
				lists.push_back(checkInStr);
			}
			
			// check lock of selected story
			for (int i = 0; i < g_TextFrameCount; i++) {
				if (g_SelectedTextFrameUID.compare(g_TextFrames[i].textframeUID) == 0) {
					//cout << "check lock of selected story / g_SelectedTextFrameUID - g_TextFrames[i].textframeUID " << g_SelectedTextFrameUID << " - " << g_TextFrames[i].textframeUID << endl;
					if (g_TextFrames[i].isLocked == TRUE) {
						// check in story
						//cout << "check in story" << endl;
						PMString checkInStoryStr(kAMPItemBaseKey);
						checkInStoryStr.AppendNumber(5); // check in story
						checkInStoryStr.Translate();
						lists.push_back(checkInStoryStr);						
					}
					else {
						// check out story
						//cout << "check out story" << endl;
						PMString checkOutStoryStr(kAMPItemBaseKey);
						checkOutStoryStr.AppendNumber(6); // check out story
						checkOutStoryStr.Translate();
						lists.push_back(checkOutStoryStr);						
					}
					break;
				}
			}
		}
		else {
			PMString signInStr(kAMPItemBaseKey);
			signInStr.AppendNumber(1); // Sign in
			signInStr.Translate();
			lists.push_back(signInStr);
			
			PMString browseIssueNodeName(kAMPItemBaseKey);
			browseIssueNodeName.AppendNumber(2); // Browse Issues
			browseIssueNodeName.Translate();
			lists.push_back(browseIssueNodeName);
		}
		
		
		
		iListData->SetStringList(lists);
	}
		
	return iListData->GetStringList().size();
}

/********************************************************************************************************************
 *****Function: GetRootNode
 *****Creator:  Toan Nguyen
 *****IN: 
 *****out: 
 *****Objective: 
 ********************************************************************************************************************/
NodeID_rv AMPTVHierarchyAdapter::GetRootNode() const
{
	//cout << "AMPTVHierarchyAdapter::GetRootNode()" << endl;
	PMString rootString("Root");
	rootString.Translate();
	return AMPNodeID::Create(rootString);
}

/********************************************************************************************************************
 *****Function: GetNthListItem
 *****Creator:  Toan Nguyen
 *****IN: 
 *****out: 
 *****Objective: 
 ********************************************************************************************************************/
NodeID_rv AMPTVHierarchyAdapter::GetNthListItem(const int32& nth) const
{
	//cout << "AMPTVHierarchyAdapter::GetNthListItem" << endl;
	InterfacePtr<IStringListData> iListData(this, IID_ISTRINGLISTDATA);

	return AMPNodeID::Create(iListData->GetStringList()[nth]);	
}

